//
//  NewsViewController.m
//  USB_Reader
//
//  Created by xiaozao on 15/3/21.
//  Copyright (c) 2015年 Tony_Wang. All rights reserved.
//

// 新闻页面首页，简洁文字新闻

#import "NewsViewController.h"
#import "SimpleNewsCell.h"
#import "NewModel.h"
#import "SimpleDetailViewController.h"
#import "ImageViewController.h"
#import "UpdataViewController.h"


@interface NewsViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UISegmentedControl *seg;
@end

@implementation NewsViewController

-(void)dealloc
{
    [_tableView release];
    [_arr release];
    [_seg release];
    [super dealloc];
}

-(void)alert:(NSString *)message{
    

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
    [alert show];
    [alert release];
}

-(void)checkNetworkStatus{
    //根据不同的网络状态改变去做相应处理
    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                [self alert:@"网络无连接"];
                break;
            
            
        }
    }];
    
    //开始监控
    [[AFNetworkReachabilityManager sharedManager] startMonitoring];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
 
    
    @autoreleasepool {
        
      
        
        self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        [self.view addSubview:self.tableView];
        
        [self.tableView registerClass:[SimpleNewsCell class] forCellReuseIdentifier:@"reuseCell"];
        
        
        self.seg = [[UISegmentedControl alloc] initWithItems:@[@"简洁资讯", @"图片资讯"]];
        self.navigationItem.titleView = self.seg;
        
        [self.seg addTarget:self action:@selector(segAction) forControlEvents:UIControlEventValueChanged];
        [self requestNetWorking];
       

         self.navigationController.navigationBar.tintColor = [UIColor blackColor];
        
        
        
    }
    

    
    // Do any additional setup after loading the view.
}

- (void)segAction
{
    if (self.seg.selectedSegmentIndex == 1) {
        
//    ImageViewController *imageVC = [[ImageViewController alloc] init];
//    [self.navigationController pushViewController:imageVC animated:YES];
//    [imageVC release];
        
        UpdataViewController *upVC = [[UpdataViewController alloc] init];
        [self.navigationController pushViewController:upVC animated:YES];
        [upVC release];
        
    }
    self.seg.selectedSegmentIndex = 0;
}



- (void)viewWillAppear:(BOOL)animated
{
    
       [self checkNetworkStatus];
    self.tabBarController.tabBar.hidden = NO;
}


- (void)requestNetWorking
{
      [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://api.yi18.net/top/list" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
     
        NSMutableArray *newsArr = [responseObject objectForKey:@"yi18"];
        self.arr = [NewModel modelArrWithDics:newsArr];
        [self.tableView reloadData];
       
        if (self.arr != nil) {
              [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}

#pragma UITableView 的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SimpleNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell" forIndexPath:indexPath];

;
    [cell setNewmodel:[self.arr objectAtIndex:indexPath.row]];

    cell.backgroundColor = [UIColor colorFromHexCode:@"#F8F8FF"];
//    cell.layer.cornerRadius = 9;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewModel *model = [self.arr objectAtIndex:indexPath.row];
    SimpleDetailViewController *simpleVC = [[SimpleDetailViewController alloc] init];
    simpleVC.model = model;
    [self.navigationController pushViewController:simpleVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
